﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class yiguzuoqi : MonoBehaviour {

    public enum TYPE
    {
        NULL  =0 ,
        MANSU = 1,
        ZHONGSU,
        KUAISU,
        YANZOUSUDU
    }

    public GameObject mansu = null;
    public GameObject zhongsu = null;
    public GameObject kuaisu = null;
    public GameObject yanzousudu = null;

    public Button btnMansuPlay = null;
    public Button btnMansuStop = null;
    public Button btnzhongsuPlay = null;
    public Button btnZhongsuStop = null;
    public Button btnKuaisuPlay = null;
    public Button btnKuaisuStop = null;
    public Button btnYanzousuduPlay = null;
    public Button btnYanzousuduStop = null;

    public AudioSource asmansu = null;
    public AudioSource aszhongsu = null;
    public AudioSource askuaisu = null;
    public AudioSource asyanzousudu = null;

    public GameObject iconmansu = null;
    public GameObject iconzhongsu = null;
    public GameObject iconkuaisu = null;
    public GameObject iconyanzousudu = null;

    public TYPE whichRotate = TYPE.NULL;

    YiguzuoqiCfgData.YiguzuoqiCfg cfg = null;


    private static yiguzuoqi instance = null;
    public static yiguzuoqi GetInstance()
    {
        return instance;
    }
    private void Awake()
    {
        instance = this;
    }


    public void Hide()
    {
        gameObject.SetActive(false);
    }
    

    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}


    private void FixedUpdate()
    {
        switch( whichRotate )
        {
            case TYPE.MANSU:
                {
                    iconmansu.transform.Rotate(new Vector3(0, 0, 1), -1f);
                }
                break;
            case TYPE.ZHONGSU:
                {
                    iconzhongsu.transform.Rotate(new Vector3(0, 0, 1), -1f);
                }
                break;
            case TYPE.KUAISU:
                {
                    iconkuaisu.transform.Rotate(new Vector3(0, 0, 1), -1f);
                }
                break;
            case TYPE.YANZOUSUDU:
                {
                    iconyanzousudu.transform.Rotate(new Vector3(0, 0, 1), -1f);
                }
                break;
        }
    }

    void AllStop()
    {
        btnMansuStop.gameObject.SetActive(false);
        btnZhongsuStop.gameObject.SetActive(false);
        btnKuaisuStop.gameObject.SetActive(false);
        btnYanzousuduStop.gameObject.SetActive(false);

        btnMansuPlay.gameObject.SetActive(true);
        btnzhongsuPlay.gameObject.SetActive(true);
        btnKuaisuPlay.gameObject.SetActive(true);
        btnYanzousuduPlay.gameObject.SetActive(true);

        if ( asmansu.isPlaying )
            asmansu.Stop();

        if ( aszhongsu.isPlaying )
            aszhongsu.Stop();

        if ( askuaisu.isPlaying )
            askuaisu.Stop();

        if ( asyanzousudu.isPlaying)
            asyanzousudu.Stop();

        whichRotate = TYPE.NULL;

        ReinitIconRotation(TYPE.MANSU);
        ReinitIconRotation(TYPE.ZHONGSU);
        ReinitIconRotation(TYPE.KUAISU);
        ReinitIconRotation(TYPE.YANZOUSUDU);
    }

    public void PlayMansu()
    {
        AllStop();

        btnMansuPlay.gameObject.SetActive(false);
        btnMansuStop.gameObject.SetActive(true);

        asmansu.Play();

        whichRotate = TYPE.MANSU;
    }

    public void PlayZhongsu()
    {
        AllStop();

        btnzhongsuPlay.gameObject.SetActive(false);
        btnZhongsuStop.gameObject.SetActive(true);

        aszhongsu.Play();

        whichRotate =TYPE.ZHONGSU;
    }

    public void PlayKuaisu()
    {
        AllStop();

        btnKuaisuPlay.gameObject.SetActive(false);
        btnKuaisuStop.gameObject.SetActive(true);

        askuaisu.Play();

        whichRotate = TYPE.KUAISU;

    }

    public void PlayYanzousudu()
    {
        AllStop();

        btnYanzousuduPlay.gameObject.SetActive(false);
        btnYanzousuduStop.gameObject.SetActive(true);

        asyanzousudu.Play();

        whichRotate = TYPE.YANZOUSUDU;
    }

    public void Stop()
    {
        ReinitIconRotation(TYPE.MANSU);
        ReinitIconRotation(TYPE.ZHONGSU);
        ReinitIconRotation(TYPE.KUAISU);
        ReinitIconRotation(TYPE.YANZOUSUDU);

        AllStop();
    }

    public void Init( string id )
    {
        gameObject.SetActive(true);

        cfg = DataManager.yiguzuoqiCfgData.GetCfg(id);
        if ( cfg == null )
        {
            return;
        }

        whichRotate = TYPE.NULL;

        ReinitIconRotation(TYPE.MANSU);
        ReinitIconRotation(TYPE.ZHONGSU);
        ReinitIconRotation(TYPE.KUAISU);
        ReinitIconRotation(TYPE.YANZOUSUDU);

        asmansu.clip = null;
        aszhongsu.clip = null;
        askuaisu.clip = null;
        asyanzousudu.clip = null;

        int iFinished = 0;

        Loading.GetInstance().StartLoading();

        if ( !string.IsNullOrEmpty(cfg.urlMansu) )
        {
            ResLoader.Load(cfg.urlMansu, null, (www) => {

                AudioClip ac = WWWAudioExtensions.GetAudioClip(www);
                asmansu.clip = ac;
                iFinished++;
                if ( iFinished >= 4 )
                {
                    Loading.GetInstance().EndLoading();
                }
            }, true, true);
        }
        else
        {
            iFinished++;
        }

        if (!string.IsNullOrEmpty(cfg.urlKuaisu))
        {
            ResLoader.Load(cfg.urlKuaisu, null, (www) => {

                AudioClip ac = WWWAudioExtensions.GetAudioClip(www);
                askuaisu.clip = ac;
                iFinished++;
                if (iFinished >= 4)
                {
                    Loading.GetInstance().EndLoading();
                }
            }, true, true);
        }
        else
        {
            iFinished++;
        }

        if (!string.IsNullOrEmpty(cfg.urlZhongsu))
        {
            ResLoader.Load(cfg.urlZhongsu, null, (www) => {

                AudioClip ac = WWWAudioExtensions.GetAudioClip(www);
                aszhongsu.clip = ac;
                iFinished++;
                if (iFinished >= 4)
                {
                    Loading.GetInstance().EndLoading();
                }
            }, true, true);
        }
        else
        {
            iFinished++;
        }

        if (!string.IsNullOrEmpty(cfg.urlYanzousudu))
        {
            ResLoader.Load(cfg.urlYanzousudu, null, (www) => {

                AudioClip ac = WWWAudioExtensions.GetAudioClip(www);
                asyanzousudu.clip = ac;
                iFinished++;
                if (iFinished >= 4)
                {
                    Loading.GetInstance().EndLoading();
                }
            }, true, true);
        }
        else
        {
            iFinished++;
        }

        AllStop();



    }


    public void OnBtnBack()
    {
        gameObject.SetActive(false);
        lessonUI.GetInstance().ShowLessonInfo();
    }

    public void ReinitIconRotation(TYPE typ)
    {
        switch(typ)
        {
            case TYPE.MANSU:
                {
                    iconmansu.transform.localRotation = Quaternion.Euler(0, 0, 0);
                }
                break;

            case TYPE.ZHONGSU:
                {
                    iconzhongsu.transform.localRotation = Quaternion.Euler(0, 0, 0);
                }
                break;

            case TYPE.KUAISU:
                {
                    iconkuaisu.transform.localRotation = Quaternion.Euler(0, 0, 0);
                }
                break;

            case TYPE.YANZOUSUDU:
                {
                    iconyanzousudu.transform.localRotation = Quaternion.Euler(0, 0, 0);
                }
                break;
        }
    }

}
